﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using HNQXYJ.DAL;
using HNQXYJ.Model;

namespace HNQXYJ.BLL
{
    public static  class TerminalBLL
    {
        /// <summary>
        /// 获取终端数据表中的数据
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static DataTable GetTerminalTable(string procName, params SqlParameter[] paras)
        {
            DataTable dt;
            if (procName != null)
            {
                dt = TerminalDAL.GetTerminalTable(procName, paras);
            }
            else
            {
                dt = null;
            }
            return dt;
        }

        /// <summary>
        /// 向终端表中插入数据
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static int InsertTerminalData(string procName, params SqlParameter[] paras)
        {
            int res = TerminalDAL.InsertTerminalData(procName, paras);
            return res;
        }

        public static int DeleteTerminalData(string procName, params SqlParameter[] paras)
        {
            int res = TerminalDAL.DeleteTerminalData(procName, paras);
            return res;
        }

        /// <summary>
        /// 通过存储过程执行非查询语句
        /// </summary>
        /// <param name="procName">存储过程</param>
        /// <param name="paras">参数</param>
        /// <returns>返回受影响的行数</returns>
        public static int ExecNoQuery(string procName, params SqlParameter[] paras)
        {
            int res = TerminalDAL.ExecNonQuery(procName, paras);
            return res;
        }

        /// <summary>
        /// 通过号码更新终端表状态
        /// </summary>
        /// <param name="phone">终端号码</param>
        /// <param name="status">终端状态</param>
        /// <returns>res</returns>
        public static int UpdateTerminalStatusByPhone(string phone, string status)
        {
            int res = TerminalBLL.UpdateTerminalStatusByPhone(phone,status);
            return res;
        }

        //根据不同的权限码返回终端数据
        public static List<Terminal> GetTerminlListByRankLevel(string ranklevel)
        {
            List<Terminal> list = TerminalDAL.GetTerminlListByRankLevel(ranklevel);
            if (list.Count > 0)
            {
                return list;
            }
            else
            {
                return null;
            }          
        }

        /// <summary>
        /// 向终端表插入数据
        /// </summary>
        /// <param name="tm"></param>
        /// <returns></returns>
        public static int InsertTerminalData(Terminal tm)
        {
            int res = TerminalDAL.InsertTerminalData(tm); 
            return res;

        }

        /// <summary>
        /// 统计各个县市的终端总数(根据类别)
        /// </summary>
        /// <param name="ranklevel">级别</param>
        /// <param name="category">类别</param>
        /// <returns>count</returns>
        public static int GetNumberALLByRankLevel(string ranklevel,string category)
        {
            int count = TerminalDAL.GetNumberALLByRankLevel(ranklevel,category);
            return count;
        }

        /// <summary>
        /// 根据传入页数和页容量的大小获取当页数据
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<Terminal> GetDataByPage(int pageNumber, int pageSize)
        {
            List<Terminal> list = TerminalDAL.GetDataByPage(pageNumber,pageSize);
            return list;
 
        }

        /// <summary>
        /// 获取终端表数据的总条数
        /// </summary>
        /// <returns></returns>
        public static int GetTerminalAllCount()
        {
            int rowCount = TerminalDAL.GetTerminalAllCount();
            return rowCount;
        }

        /// <summary>
        /// 返回总页数
        /// </summary>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static int GetTerminalPageCount(int pageSize)
        {
            int rows = GetTerminalAllCount();
            return (int)Math.Ceiling(rows/(double)pageSize);
        }

         /// <summary>
        /// 统计出每个城市下的各个县市区的终端的状态数据
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="ranklevel">级别</param>
        /// <param name="category">类别</param>
        /// <returns>count</returns>
        public static int GetNumberByStatusAndCounty(string status, string ranklevel, string category)
        {
            int count = TerminalDAL.GetNumberByStatusAndCounty(status,ranklevel,category);
            return count;
        }

        /// <summary>
        /// 专为泗阳做的终端号码提取方法。
        /// </summary>
        /// <returns></returns>
        public static List<string> GetPhoneForSendMsgToSiYang()
        {

            List<string> list = TerminalDAL.GetPhoneForSendMsgToSiYang();          
            return list;

        }

         /// <summary>
        /// 分页存储过程
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="rowCount">总行数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public static List<Terminal> TerminalPagingList(int pageIndex, int pageSize, string strWhere, out int rowCount, out int pageCount)
        {
            List<Terminal> list = TerminalDAL.TerminalPagingList(pageIndex, pageSize, strWhere, out rowCount, out pageCount);
            return list;
        }

        /// <summary>
        /// 带权限的分页存储过程
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">也容量</param>
        /// <param name="townName">村镇名称</param>
        /// <param name="rankLevel">权限码</param>
        /// <param name="rowCount">总行数</param>
        /// <param name="pageCount">总页数</param>
        /// <returns>list</returns>
        public static List<Terminal> TerminalPagedRankLevel(int pageIndex, int pageSize, string townName, string rankLevel, out int rowCount, out int pageCount)
        {
            List<Terminal> list = TerminalDAL.TerminalPagedRankLevel(pageIndex, pageSize, townName, rankLevel,out rowCount,out pageCount);
            return list;
        }

        /// <summary>
        /// 根据权限码来获取终端号码
        /// </summary>
        /// <param name="ranklevel"></param>
        /// <returns></returns>
        public static List<string> GetPhonesByRankLevel(string ranklevel)
        {
            List<string> list = TerminalDAL.GetPhonesByRankLevel(ranklevel);
            return list;
        }
        
        /// <summary>
        /// 根据分组代码提取终端号码
        /// </summary>
        /// <param name="areacode"></param>
        /// <returns></returns>
        public static List<string> GetPhonesByAreaCode(string areacode)
        {
            List<string> list = TerminalDAL.GetPhonesByAreaCode(areacode);
            return list;
        }

        /// <summary>
        /// 通过终端号码来更新其所在分组
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public static int UpateAreaCodeByPhone(string phone, string areaCode)
        {
            int res = TerminalDAL.UpateAreaCodeByPhone(phone,areaCode);
            return res;
        }

         /// <summary>
        /// 根据id获取一个终端对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Terminal GetDataById(int id)
        {
            Terminal tm = TerminalDAL.GetDataById(id);
            return tm;
        }

        /// <summary>
         /// 更新一个终端
         /// </summary>
         /// <param name="tm"></param>
         /// <returns></returns>
        public static int UpdateTerminalById(Terminal tm)
        {
            int res = TerminalDAL.UpdateTerminalById(tm);
            return res;
        }

        /// <summary>
        /// 返回终端列表 只有终端号码和状态
        /// </summary>
        /// <returns></returns>
        public static List<Terminal> GetPhoneAndStatus()
        {
            List<Terminal> list = TerminalDAL.GetPhoneAndStatus();
            return list;
        }

    }
}